package com.ruoyi.life.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.life.domain.FeApartmentCancelBill;
import com.ruoyi.life.domain.SfReceiptDetail;
import com.ruoyi.life.mapper.FeApartmentBillMapper;
import com.ruoyi.life.mapper.FeApartmentCancelBillMapper;
import com.ruoyi.life.mapper.SfReceiptDetailMapper;
import com.ruoyi.life.service.IFeBillService;
import com.ruoyi.life.service.ISfReceiptDetailService;
import com.ruoyi.omp.api.domain.FeApartmentBill;
import com.ruoyi.omp.api.domain.FeBill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收款明细Service业务层处理
 *
 * @author cjie
 * @date 2021-08-03
 */
@Service
public class SfReceiptDetailServiceImpl implements ISfReceiptDetailService {
    @Autowired
    private SfReceiptDetailMapper sfReceiptDetailMapper;
    @Resource
    private IFeBillService feBillService;

    @Autowired
    private FeApartmentCancelBillMapper feApartmentCancelBillMapper;

    @Autowired
    private FeApartmentBillMapper feApartmentBillMapper;


    /**
     * 查询收款明细
     *
     * @param id 收款明细ID
     * @return 收款明细
     */
    @Override
    public SfReceiptDetail selectSfReceiptDetailById(String id) {
        return sfReceiptDetailMapper.selectSfReceiptDetailById(id);
    }

    /**
     * 查询收款明细列表
     *
     * @param sfReceiptDetail 收款明细
     * @return 收款明细
     */
    @Override
    public List<SfReceiptDetail> selectSfReceiptDetailList(SfReceiptDetail sfReceiptDetail) {
        if(StringUtils.isNotBlank(sfReceiptDetail.getBillType()) && sfReceiptDetail.getBillType().equals("公寓账单")){
            List<SfReceiptDetail> results = sfReceiptDetailMapper.selectApartSfReceiptDetailList(sfReceiptDetail);
            return results;
        }
        return sfReceiptDetailMapper.selectSfReceiptDetailList(sfReceiptDetail);
    }

    /**
     * 新增收款明细
     *
     * @param sfReceiptDetail 收款明细
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSfReceiptDetail(SfReceiptDetail sfReceiptDetail) {
        sfReceiptDetail.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        sfReceiptDetail.setCreateTime(DateUtils.getNowDate());
        sfReceiptDetail.setCreateBy(SecurityUtils.getUsername());
        if(StringUtils.isNotBlank(sfReceiptDetail.getBillType()) && sfReceiptDetail.getBillType().equals("公寓退租账单")){
            if(StringUtils.isNotEmpty(sfReceiptDetail.getBillId())){
                FeApartmentCancelBill cancelBill = feApartmentCancelBillMapper.selectFeApartmentCancelBillById(sfReceiptDetail.getBillId());
                //增加已收账单金额
                cancelBill.setActualAmount(cancelBill.getActualAmount().add(sfReceiptDetail.getReceiptAmount()));
                //增加已收违约金
                if(sfReceiptDetail.getReceiptTotalFine() !=null){
                    cancelBill.setActualFine(cancelBill.getActualFine().add(sfReceiptDetail.getReceiptTotalFine()));
                }
                //已收金额大于等于应收，该状态为已支付  账单金额和违约金
                if(cancelBill.getActualAmount().compareTo(cancelBill.getBillMoney())>-1&& cancelBill.getActualFine().compareTo(cancelBill.getFine())>-1){
                    cancelBill.setPayState("2");
                    cancelBill.setBillState("2");
                }else {
                    if(cancelBill.getPayState().equals("0")){
                        cancelBill.setPayState("1");
                    }
                }
                feApartmentCancelBillMapper.updateFeApartmentCancelBill(cancelBill);
            }
            return sfReceiptDetailMapper.insertSfReceiptDetail(sfReceiptDetail);
        }else if(StringUtils.isNotBlank(sfReceiptDetail.getBillType()) && sfReceiptDetail.getBillType().equals("公寓账单")){
            if(StringUtils.isNotEmpty(sfReceiptDetail.getBillId())){
                FeApartmentBill apartBill = feApartmentBillMapper.selectFeApartmentBillById(sfReceiptDetail.getBillId());
                //增加已收账单金额
                apartBill.setPayAmount(apartBill.getPayAmount().add(sfReceiptDetail.getReceiptAmount()));
                //增加已收违约金
                if(sfReceiptDetail.getReceiptTotalFine() !=null){
                    apartBill.setBreakFeePay(apartBill.getBreakFeePay().add(sfReceiptDetail.getReceiptTotalFine()));
                }
                //已收金额大于等于应收，该状态为已支付  账单金额和违约金
                if(apartBill.getPayAmount().compareTo(apartBill.getReceivableAmount())>-1&& apartBill.getBreakFeePay().compareTo(apartBill.getTotalFine())>-1){
                    apartBill.setPayState("2");
                    apartBill.setBillState("2");
                }else{
                    if(apartBill.getPayState().equals("0")){
                        apartBill.setPayState("1");
                    }

                }
                feApartmentBillMapper.updateFeApartmentBill(apartBill);
            }
            return sfReceiptDetailMapper.insertSfReceiptDetail(sfReceiptDetail);
        }else{
            if(StringUtils.isNotEmpty(sfReceiptDetail.getBillId())){
                FeBill feBill = feBillService.selectFeBillById(sfReceiptDetail.getBillId());
                //增加已收账单金额
                feBill.setPayAmount(feBill.getPayAmount().add(sfReceiptDetail.getReceiptAmount()));
                //增加已收违约金
                feBill.setBreakFeePay(feBill.getBreakFeePay().add(sfReceiptDetail.getReceiptTotalFine()));
                //已收金额大于等于应收，该状态为已支付  账单金额和违约金
                if (feBill.getPayAmount().compareTo(feBill.getReceivableAmount())>-1&&feBill.getBreakFeePay().compareTo(feBill.getTotalFine())>-1){
                    feBill.setPayState("2");
                    feBill.setRemark("正常结清");
                }else {
                    if(feBill.getPayState().equals("0")){
                        feBill.setPayState("1");
                    }
                }
                feBillService.updateFeBill(feBill);
            }

            return sfReceiptDetailMapper.insertSfReceiptDetail(sfReceiptDetail);
        }

    }

    /**
     * 修改收款明细
     *
     * @param sfReceiptDetail 收款明细
     * @return 结果
     */
    @Override
    public int updateSfReceiptDetail(SfReceiptDetail sfReceiptDetail) {
        sfReceiptDetail.setUpdateTime(DateUtils.getNowDate());
        sfReceiptDetail.setUpdateBy(SecurityUtils.getUsername());
        return sfReceiptDetailMapper.updateSfReceiptDetail(sfReceiptDetail);
    }

    /**
     * 批量删除收款明细
     *
     * @param ids 需要删除的收款明细ID
     * @return 结果
     */
    @Override
    public int deleteSfReceiptDetailByIds(String[] ids) {
        return sfReceiptDetailMapper.deleteSfReceiptDetailByIds(ids);
    }

    /**
     * 删除收款明细信息
     *
     * @param id 收款明细ID
     * @return 结果
     */
    @Override
    public int deleteSfReceiptDetailById(String id) {
        return sfReceiptDetailMapper.deleteSfReceiptDetailById(id);
    }
}
